﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib;
using System.IO.Ports;
using System.Windows.Forms;

namespace 地声电源电路控制软件
{
    public class CP1616Packet
    {
        public static bool OpenSerialPort(ref SerialPort mySerialPort, string name)
        {
            while (true)
            {
                try
                {
                    mySerialPort.Open();
                    break;
                }
                catch (System.Exception ex)
                {
                    if (MessageBox.Show("串口无法打开:" + ex.Message + "，是否重试?", "错误", MessageBoxButtons.RetryCancel) == DialogResult.Cancel)
                        return false;
                }
            }
            return true;
        }
        public int RxCount;
        public byte[] RxList;
        public byte NeedCommand;
        public byte NeedRxCount;
        public static Int32 BUFFER_SIZE = 1024;
        public CP1616Packet(byte com)
        {
            NeedCommand=com;
            RxCount = 0;
            RxList = new byte[100];
        }
        public CP1616Packet()
        {
            NeedCommand=0xff;
            RxCount = 0;
            RxList = new byte[100];
        }
        
        public static byte[] MakeCP1616Packet(byte com, byte[] data)
        {
            byte[] txbuffer;
            if(data!=null)
            {
                txbuffer = new byte[4 + data.Length + 2];
                txbuffer[0] = 0x16;
                txbuffer[1] = 0x16;
                txbuffer[2] = com;
                txbuffer[3] = (byte)data.Length;
                for (int i = 0; i < data.Length; i++)
                {
                    txbuffer[i + 4] = data[i];
                }
                txbuffer[4 + data.Length] = Verify.GetVerify_byteSum(txbuffer, 4 + data.Length);
                txbuffer[4 + data.Length + 1] = 0x0d;
            }
            else
            {
                txbuffer = new byte[4+ 2];
                txbuffer[0] = 0x16;
                txbuffer[1] = 0x16;
                txbuffer[2] = com;
                txbuffer[3] = 0x00;
                txbuffer[4] = Verify.GetVerify_byteSum(txbuffer, 4);
                txbuffer[5] = 0x0d;
            }            
            return txbuffer;
        }
        public bool DataPacketed(byte rx)
        {
            RxList[RxCount++] = rx;
            if (RxCount > 4)
            {
                if (rx == 0x0d && RxCount == NeedRxCount)
                {
                    return true;
                }
                else if (RxCount > NeedRxCount)
                {
                    RxCount = 0;
                    NeedRxCount = 0xff;
                }
                else if (RxCount > 20)
                {
                    RxCount = 0;
                    NeedRxCount = 0xff;
                }
            }
            else if (RxCount == 4)
            {
                NeedRxCount = (byte)(RxList[3] + 6);
            }
            else if (RxCount == 2)
            {
                if (rx != 0x16)
                {
                    RxCount = 0;
                }
            }
            else if (RxCount == 1)
            {
                if (rx != 0x16)
                {
                    RxCount = 0;
                }
            }            
            return false;
        }        
    }

}
